import {reactive, ref, toRefs, getCurrentInstance, watch} from "vue"

export default function () {
    const {proxy} = getCurrentInstance()

    const totalNums = ref(0)
    const page = ref(1)
    const pageSize = ref(20) // 每页显示几条
    const pageSizeNum = ref([20, 30, 40]) //调整每页显示条数

    const apiPath = ref('')

    const searchParams = reactive({})
    const searchBtnLoading = ref(false)

    let result = reactive({
        list: []
    })
    const loading = ref(false)

    const dialogShow = ref(false)
    const dialogForm = reactive({})
    const dialogFormRef = ref(null)
    const dialogParams = reactive({
        addApiPath: '',
        editApiPath: '',
        type: '',
        loading: false,
        data:{}
    })

    const handleAdd = (data) => {
        dialogShow.value = true
        dialogParams.type = 'add'
        dialogParams.data = data || {}
        resetDialogForm(dialogFormRef.value)
    }
    const handleEdit = (row,data) => {
        dialogShow.value = true
        dialogParams.type = 'edit'
        dialogParams.data = data
        setTimeout(() => {
            for (let key in dialogForm) {
                dialogForm[key] = row[key]
            }
        }, 50)
    }
    const resetDialogForm = (formEl) => {
        if (!formEl) return
        // console.log(formEl);
        formEl.resetFields()
    }
    const submitDialogForm = (formEl,params) => {
        if (!formEl) return
        formEl.validate((valid, fields) => {
            if (valid) {
                dialogParams.loading = true
                let data;
                if(params){
                    data = params
                }else{
                    data = dialogForm
                }
                let url = '';
                if (dialogParams.type == 'add') {
                    url = dialogParams.addApiPath
                }
                if (dialogParams.type == 'edit') {
                    url = dialogParams.editApiPath
                }
                data = {
                    ...data,
                    ...dialogParams.data
                }
                proxy.$axios.post(url, data)
                    .then((res) => {
                        dialogParams.loading = false
                        if (res.data.code == 200) {
                            closeDialog()
                            proxy.$message({
                                message: `${dialogParams.type == 'add' ? "添加成功！" : "编辑成功！"}`,
                                type: "success"
                            })
                            getData()
                        } else {
                            proxy.$message({
                                message: res.data.msg,
                                type: "error"
                            })
                        }
                    })
                    .catch((err) => {
                        dialogParams.loading = false
                    })
            }
        })
    }
    const closeDialog = () => {
        dialogShow.value = false
        resetDialogForm(dialogFormRef.value)
    }

    const handleAction = (url, data) => {
        proxy.$axios.post(url, data)
            .then((res) => {
                if (res.data.code == 200) {
                    proxy.$message({
                        message: "操作成功",
                        type: "success"
                    })
                    getData()
                } else {
                    proxy.$message({
                        message: res.data.msg,
                        type: "error"
                    })
                }
            })
            .catch((err) => {
            })
    }

    function handleAllSelected() {

    }

    const handleSearch = async () => {
        searchBtnLoading.value = true
        await getData();
        searchBtnLoading.value = false
    }

    const getData = async () => {
        let params = {
            page: page.value,
            limit: pageSize.value,
            ...searchParams
        }
        loading.value = true;
        console.log('getData', apiPath.value);
        await proxy.$axios
            .get(apiPath.value, {params: params})
            .then((res) => {
                switch (res.data.code) {
                    case 200:
                        let resData = res.data.data
                        result.list = resData.data
                        // result.list = [{is_stop: 1}]
                        totalNums.value = resData.total
                        break
                    default:
                        proxy.$message({message: res.data.msg, type: "error"})
                        break
                }
                loading.value = false;
            })
            .catch((error) => {
                loading.value = false;
            })
    }

    // 切换每页显示条数
    watch(pageSize, async (val) => {
        page.value = 1
        await getData()
    })
    // 跳转第几页
    watch(page, async (val) => {
        await getData()
    })

    return {
        page,
        pageSize,
        pageSizeNum,
        totalNums,
        searchParams,
        searchBtnLoading,
        apiPath,
        handleSearch,
        getData,
        loading, ...toRefs(result),
        dialogFormRef,
        dialogShow,
        handleAdd,
        handleEdit,
        resetDialogForm,
        handleAllSelected,
        dialogForm,
        submitDialogForm,
        dialogParams,
        closeDialog,
        handleAction
    }
}
